package org.bob.learn.consumer.api.producer;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = "producer",configuration = ProducerApiService.RequestHeaderInterceptor.class,fallbackFactory = ProducerApiServiceFallbackFactory.class)
public interface ProducerApiService {

    @Cacheable
    @GetMapping("/producer/msg")
    String msg(@RequestParam Long id);

    /**
     *  HTTP请求头部拦截器
     */
    @Slf4j
    @Component
    class RequestHeaderInterceptor implements RequestInterceptor {

        /**
         * 设置头部
         *
         * @param requestTemplate 请求模板
         */
        @Override
        public void apply(RequestTemplate requestTemplate) {
            requestTemplate.header("Content-Type", "application/json;charset=UTF-8");
            requestTemplate.header("Accept", "application/json");

        }
    }
}
